package a;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class CmdDemo {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            // 提示用户输入命令
            System.out.print(">");
            String cmd = scanner.nextLine();
            // 创建ProcessBuilder对象
            ProcessBuilder pb = new ProcessBuilder();
            // 设置命令和参数
            pb.command("cmd.exe", "/c", cmd);
            // 启动进程
            Process process = pb.start();
            // 获取进程的输出流
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            // 读取输出并打印到控制台
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            // 等待进程结束
            try {
                int exitCode = process.waitFor();
                System.out.println("Process exit with code " + exitCode);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
